package com.library.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.domain.pojo.Book;
import com.library.domain.pojo.LendRecord;
import com.library.mapper.BookMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : 凉小天
 * @date : 2021/6/30 16:59
 */
@Service("bookService")
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Resource
    private LendRecordService lendRecordService;

    /**
     * 分页查询图书
     * page 分页   book 查询信息
    */

    @Override
    public Map<String, Object> page(Page<Book> page, Book book) {
        String author = book.getAuthor();
        String bookName = book.getBookName();

        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!StringUtils.isEmpty(author), Book::getAuthor,author)
                .like(!StringUtils.isEmpty(bookName), Book::getBookName,bookName);
        Page<Book> selectPage = this.baseMapper.selectPage(page,wrapper);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",selectPage.getRecords());
        map.put("total",selectPage.getTotal());
        return map;
    }

    /**
     * 借阅图书
     * 根据图书的ISBN编号借阅图书
     * 如果当前图书未被借阅(status为0) ,修改status为1 , 同时新增一条借阅记录
     * @param isbn
     * @param readerId
     * @return
     */
    @Override
    public boolean lendBook(String isbn, Long readerId) {
        Book book = this.baseMapper.selectById(isbn);
        book.setStatus("1");
        int res = this.baseMapper.updateById(book);

        //x新增借阅记录
        LendRecord lendRecord = new LendRecord();
        lendRecord.setIsbn(isbn);
        lendRecord.setReaderId(readerId);
        lendRecord.setLendDate(new Date());
        boolean save = lendRecordService.save(lendRecord);

        return res == 1 && save;
    }

    /**
     * 归还图书
     * @param isbn
     * @param readerId
     * @return
     */
    @Override
    public boolean returnBook(String isbn, Long readerId) {
        Book book = this.baseMapper.selectById(isbn);
        book.setStatus("0");
        int res = this.baseMapper.updateById(book);

        //gen跟新借阅记录状态
        QueryWrapper<LendRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(LendRecord::getStatus,"0").eq(LendRecord::getIsbn,isbn).eq(LendRecord::getReaderId,readerId);
        LendRecord lendRecord = lendRecordService.getOne(wrapper);
        lendRecord.setReturnDate(new Date());
        lendRecord.setStatus("1");
        boolean update = lendRecordService.update(lendRecord,wrapper);
     return res == 1 &&update;
    }
}
